package com.sky.service.impl;

import com.sky.dto.GoodsSalesDTO;
import com.sky.mapper.OrdersMapper;
import com.sky.mapper.UserMapper;
import com.sky.service.ReportService;
import com.sky.vo.*;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private OrdersMapper ordersMapper;
    @Autowired
    private UserMapper userMapper;
    /**
     * 营业数据统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO getTurnover(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);

        while (!begin.equals(end)){
            begin = begin.plusDays(1);//日期计算，获得指定日期后1天的日期
            dateList.add(begin);
        }

        //y轴数据
        List<Double> turnoverList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);
            Map map = new HashMap();
            map.put("status",5);
            map.put("begin",beginTime);
            map.put("end",endTime);
            Double amout = ordersMapper.getOrderAmout(map);
            amout = amout == null ? 0.0 : amout;
            turnoverList.add(amout);
        }
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .turnoverList(StringUtils.join(turnoverList,","))
                .build();
        return turnoverReportVO;
    }

    /**
     * 得到用户报表数据
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO getUserStatus(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        //x轴时间数据
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }

        List<Long> newUserList = new ArrayList<>();
        List<Long> newtotalUserList = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate,LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate,LocalTime.MAX);
            //得到新增用户单日
            Long countUser = userMapper.getCountUser(beginTime,endTime);
            countUser = countUser != null ? countUser : 0;
            newUserList.add(countUser);
            //得到单日用户总量
            Long countTotal = userMapper.getCountUser(null,endTime);
            countTotal = countTotal != null ? countTotal : 0;
            newtotalUserList.add(countTotal);
        }
        UserReportVO build = UserReportVO.builder()
                .dateList(StringUtils.join(dateList, ","))
                .newUserList(StringUtils.join(newUserList, ","))
                .totalUserList(StringUtils.join(newtotalUserList, ","))
                .build();
        return build;
    }

    @Override
    public OrderReportVO getOrderStatistics(LocalDate begin, LocalDate end) {
        List<LocalDate> dateList = new ArrayList<>();
        dateList.add(begin);
        while (!begin.equals(end)) {
            begin = begin.plusDays(1);
            dateList.add(begin);
        }
        //每天订单总数集合
        List<Long> orderCounts = new ArrayList<>();
        // 每天有效订单数集合
        List<Long> orderCountHelpfors = new ArrayList<>();
        for (LocalDate localDate : dateList) {
            LocalDateTime beginTime = LocalDateTime.of(localDate, LocalTime.MIN);
            LocalDateTime endTime = LocalDateTime.of(localDate, LocalTime.MAX);

            //查询每天的总订单数 select count(id) from orders where order_time > ? and order_time < ?
            Long orderCount = ordersMapper.getOrderCount(beginTime, endTime,null);
            orderCount = orderCount != null ? orderCount : 0;
            orderCounts.add(orderCount);
            //查询每天的有效订单数 select count(id) from orders where order_time > ? and order_time < ? and status = ?
            Long orderCountHelpfor = ordersMapper.getOrderCount(beginTime, endTime,5);
            orderCountHelpfor = orderCountHelpfor != null ? orderCountHelpfor : 0;
            orderCountHelpfors.add(orderCountHelpfor);
        }

        //时间区间内的总订单数
        Integer totalOrderCount = Math.toIntExact(orderCounts.stream().reduce(Long::sum).get());
        //时间区间内的总有效订单数
        Integer validOrderCount = Math.toIntExact(orderCountHelpfors.stream().reduce(Long::sum).get());
        //订单完成率
        Double orderCompletionRate = validOrderCount.doubleValue() / validOrderCount;
        OrderReportVO build = OrderReportVO.builder()
                .dateList(StringUtils.join(dateList,","))
                .orderCountList(StringUtils.join(orderCounts,","))
                .validOrderCountList(StringUtils.join(orderCountHelpfors,","))
                .totalOrderCount(totalOrderCount)
                .validOrderCount(validOrderCount)
                .orderCompletionRate(orderCompletionRate)
                .build();
        return build;
    }

    @Override
    public SalesTop10ReportVO getSalesTop10(LocalDate begin, LocalDate end) {
        //获取开始结束时间
        LocalDateTime beginTime = LocalDateTime.of(begin,LocalTime.MIN);
        LocalDateTime endTime = LocalDateTime.of(end,LocalTime.MAX);

        //查询获取状态已完成的top10
        List<GoodsSalesDTO> orderList = ordersMapper.SalesTop10(beginTime,endTime);
        System.out.println(orderList);
        String nameList = StringUtils.join(orderList.stream().map(o -> o.getName()).collect(Collectors.toList()), ",");
        String numberList = StringUtils.join(orderList.stream().map(o -> o.getNumber()).collect(Collectors.toList()), ",");
        SalesTop10ReportVO salesTop10ReportVO = SalesTop10ReportVO.builder()
                .nameList(nameList)
                .numberList(numberList)
                .build();
        return salesTop10ReportVO;
    }
}
